/**
 * 非专业人士请勿修改该文件;
 * js工具函数类,里面还有些地方没有修改完成;
 */
//window.global = window.global || {};
//var core = cc.Class({
class Core {
  isObject(param) {
    return typeof param === "object";
  }
  isArray(param) {
    return typeof param === "Array" || param instanceof Array;
  }
  isNumber(param) {
    return !isNaN(param);
  }
  isNaN(param) {
    return isNaN(param);
  }
  isFunction(param) {
    return typeof param === "function";
  }
  isString(param) {
    return typeof param === "string";
  }
  isUndefined(param) {
    if (cc.vv.core.isObject(param)) {
      return false;
    }
    if (cc.vv.core.isArray(param)) {
      return false;
    }
    if (cc.vv.core.isNumber(param)) {
      return false;
    }
    if (cc.vv.core.isFunction(param)) {
      return false;
    }
    if (cc.vv.core.isString(param)) {
      return false;
    }
    return true;
  }
  random(min, max) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * min + 1);
      case 2:
        return parseInt(Math.random() * (max - min + 1) + min);
      default:
        return math.random();
    }
  }
  clone(obj) {
    if (cc.vv.core.isUndefined(obj)) {
      return null;
    }
    if (cc.vv.core.isArray(obj)) {
      var newArray = new Array();
      for (var i = 0, length = obj.length; i < length; ++i) {
        newArray[i] = cc.vv.core.clone(obj[i]);
      }
      return newArray;
    }
    if (cc.vv.core.isObject(obj)) {
      var newObj = new Object();
      for (var i in obj) {
        newObj[i] = cc.vv.core.clone(obj[i]);
      }
      return newObj;
    }
    return obj;
  }
  //合并但是不改变dest
  merge(dest, src) {
    var t = cc.vv.core.clone(dest);
    if (!src) {
      return t;
    }
    if (cc.vv.core.isArray(src)) {
      t = t || [];
      var tlen = t.length;
      for (var i = 0, len = src.length; i < len; ++i) {
        var value = src[i];
        if (cc.vv.core.isFunction(value)) {
          continue;
        }
        if (cc.vv.core.isArray(value) || cc.vv.core.isObject(value)) {
          if (i < tlen) {
            t[i] = cc.vv.core.merge(null, value);
          } else {
            t.push(cc.vv.core.merge(null, value));
          }
        } else {
          if (i < tlen) {
            t[i] = value;
          } else {
            t.push(value);
          }
        }
      }
    } else if (cc.vv.core.isObject(src)) {
      t = t || {};
      for (var key in src) {
        var value = src[key];
        if (cc.vv.core.isFunction(value)) {
          continue;
        }
        if (cc.vv.core.isArray(value) || cc.vv.core.isObject(value)) {
          t[key] = cc.vv.core.merge(null, value);
        } else {
          t[key] = value;
        }
      }
    } else {
      t = src;
    }
    return t;
  }
  //只是取出template里面已经有的部分
  getExit(template, src) {
    if (!src || !template) {
      return {};
    }
    var t = {};
    for (var key in src) {
      if (cc.vv.core.isUndefined(template[key])) {
        continue;
      }
      var value = src[key];
      if (cc.vv.core.isFunction(value)) {
        continue;
      }
      if (cc.vv.core.isArray(value) || cc.vv.core.isObject(value)) {
        t[key] = cc.vv.core.getExit(template[key], value);
      } else {
        t[key] = value;
      }
    }
    return t;
  }
  dump(data) {
    var strSpace = "    ";
    var _dump = function(data, intent) {
      var startIntentStr = "";
      for (var i = 0; i < intent; ++i) {
        startIntentStr += strSpace;
      }
      var contentStr = "";
      if (cc.vv.core.isArray(data)) {
        contentStr += "\n" + startIntentStr + "[\n";
        for (var i = 0, length = data.length; i < length; ++i) {
          if (
            !cc.vv.core.isArray(data[i]) &&
            !cc.vv.core.isObject(data[i]) &&
            !cc.vv.core.isFunction(data[i])
          ) {
            contentStr +=
              startIntentStr + strSpace + _dump(data[i], intent + 1) + ",\n";
          } else {
            contentStr += startIntentStr + _dump(data[i], intent + 1) + ",\n";
          }
        }
        contentStr += startIntentStr + "]";
      } else if (cc.vv.core.isObject(data)) {
        contentStr += startIntentStr + "{\n";
        for (var key in data) {
          contentStr +=
            startIntentStr +
            strSpace +
            key +
            ":" +
            _dump(data[key], intent + 1) +
            ",\n";
        }
        contentStr += startIntentStr + "}";
      } else if (!cc.vv.core.isFunction(data)) {
        if (cc.vv.core.isString(data)) {
          return '"' + data + '"';
        }
        return data;
      }
      if (contentStr == "") {
        return "";
      }
      return contentStr;
    };
    console.log(_dump(data, 0));
  }

  toArray(table) {
    var array = [];
    for (var key in table) {
      if (cc.vv.core.isFunction(table[key])) {
        continue;
      }
      array.push(key);
      array.push(table[key]);
    }
    return array;
  }

  foreach(items, callback) {
    if (!items || !callback) {
      return;
    }
    if (cc.vv.core.isNumber(items)) {
      for (var i = 0; i < items; ++i) {
        var ret = callback(i);
        if (ret) {
          return;
        }
      }
    } else if (cc.vv.core.isString(items)) {
      for (var i = 0, length = items.length; i < length; i++) {
        var ret = callback(i, items.charAt(i));
        if (ret) {
          return;
        }
      }
    } else if (cc.vv.core.isArray(items)) {
      for (var i = 0, length = items.length; i < length; ++i) {
        var ret = callback(i, items[i]);
        if (ret) {
          return;
        }
      }
    } else if (cc.vv.core.isObject(items)) {
      for (var key in items) {
        var ret = callback(key, items[key]);
        if (ret) {
          return;
        }
      }
    }
    return;
  }

  length(items) {
    if (cc.vv.core.isArray(items)) {
      return items.length;
    }
    if (cc.vv.core.isObject) {
      var len = 0;
      cc.vv.core.foreach(items, function() {
        ++len;
      });
      return len;
    }
    return 0;
  }
  find(items, callback) {
    if (!items || !callback) {
      return;
    }
    if (cc.vv.core.isArray(items)) {
      for (var i = 0, length = items.length; i < length; ++i) {
        if (callback(items[i]) === true) {
          return items[i];
        }
      }
    } else if (cc.vv.core.isObject(items)) {
      for (var key in items) {
        if (callback(items[key]) === true) {
          return items[key];
        }
      }
    }
  }

  indexOf(items, value) {
    if (!items || !value) {
      return -1;
    }
    if (cc.vv.core.isArray(items)) {
      for (var i = 0, length = items.length; i < length; ++i) {
        if (items[i] === value) {
          return i;
        }
      }
    } else if (cc.vv.core.isObject(items)) {
      for (var key in items) {
        if (items[key] === value) {
          return key;
        }
      }
    }
    return -1;
  }
  include(objs, key) {
    var r = false;
    cc.vv.core.foreach(objs, function(k, obj) {
      if (k == key) {
        r = true;
        return true;
      }
    });
    return r;
  }
  supportsWebSocket() {
    return window.WebSocket || window.MozWebSocket;
  }

  userAgentContains(string) {
    return navigator.userAgent.indexOf(string) != -1;
  }

  isTablet(screenWidth) {
    if (screenWidth > 640) {
      if (
        (cc.vv.core.userAgentContains("Android") &&
          cc.vv.core.userAgentContains("Firefox")) ||
        cc.vv.core.userAgentContains("Mobile")
      ) {
        return true;
      }
    }
    return false;
  }

  isWindows() {
    return cc.vv.core.userAgentContains("Windows");
  }

  isChromeOnWindows() {
    return (
      cc.vv.core.userAgentContains("Chrome") &&
      cc.vv.core.userAgentContains("Windows")
    );
  }

  canPlayMP3() {
    return Modernizr.audio.mp3;
  }

  isSafari() {
    return (
      cc.vv.core.userAgentContains("Safari") &&
      !cc.vv.core.userAgentContains("Chrome")
    );
  }

  isOpera() {
    return cc.vv.core.userAgentContains("Opera");
  }
  isInt(n) {
    return n % 1 === 0;
  }

  removeAt(arr, index) {
    if (typeof arr === "undefined" || typeof index === "undefined") {
      return arr;
    }
    if (!cc.vv.core.isArray(arr)) {
      return arr;
    }
    var len = arr.length;
    if (isNaN(index) || index >= len || index < 0) {
      return arr;
    }
    for (var i = 0; i < len - 1; ++i) {
      if (i < index) {
        continue;
      } else {
        arr[i] = arr[i + 1];
      }
    }
    arr.length -= 1;
    return arr;
  }
  char2buf(str) {
    var out = new ArrayBuffer(str.length * 2);
    var u16a = new Uint16Array(out);
    var strs = str.split("");
    for (var i = 0; i < strs.length; i++) {
      u16a[i] = strs[i].charCodeAt();
    }
    return out;
  }

  array2arraybuffer(array) {
    var b = new ArrayBuffer(array.length);
    var v = new DataView(b, 0);
    for (var i = 0; i < array.length; i++) {
      v.setUint8(i, array[i]);
    }
    return b;
  }

  arraybuffer2array(buffer) {
    var v = new DataView(buffer, 0);
    var a = new Array();
    for (var i = 0; i < v.byteLength; i++) {
      a[i] = v.getUint8(i);
    }
    return a;
  }

  firstCharUpCase(str) {
    var reg = /\b(\w)|\s(\w)/g;
    str = str.toLowerCase();
    return str.replace(reg, function(m) {
      return m.toUpperCase();
    });
  }

  getGoogleProtoSetName(str) {
    var strs = str.split("_");
    var result = "set";
    cc.vv.core.foreach(strs, function(index, s) {
      result += cc.vv.core.firstCharUpCase(s);
    });
    return result;
  }

  convertIntToUInt8Array(num) {
    var c2 = num % 256;
    var c1 = Math.floor(num / 256);

    var c = [c1, c2];
    return c;
  }

  convertUInt8ArrayToInt(array) {
    if (array.length != 2) {
      return 0;
    }
    return array[0] * 256 + array[1];
  }
  //从一个数组里面按照下标和长度的方式去获取另一个数组，至少返回空数组
  //包含start
  getArrayFromArray(data, start, l) {
    var len = data.length;
    start = start || 0; //默认最开始
    l = l || len; //默认到最后
    if (start >= len) {
      return [];
    }
    var arr = [];
    for (var i = start; i < start + l && i < len; ++i) {
      arr.push(data[i]);
    }
    return arr;
  }

  blobToArrayBuffer(data, callback) {
    if (cc.sys.isNative || cc.sys.platform === cc.sys.WECHAT_GAME) {
      callback(data);
      return;
    }
    var fileReader = new FileReader();
    fileReader.onload = function(progressEvent) {
      if (callback) {
        callback(this.result);
      }
    };
    fileReader.readAsArrayBuffer(data);
  }
}
export let core: Core = new Core();
